package service

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"time"
)

type NewList struct {
	MID   int `json:"mid"`
	AID   int `json:"aid"`
	ID    int `json:"id"`
	Page  int `json:"page"`
	Limit int `json:"limit"`
}

// 地址列表
func GetAddList(c *gin.Context) {
	//接收
	var jsonData NewList
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	//查询
	var list []models.MMemberAddress
	var count int64
	if jsonData.Page == 0 {
		jsonData.Page = -1
	}
	if jsonData.Limit == 0 {
		jsonData.Limit = -1
	}
	offsetVal := (jsonData.Page - 1) * jsonData.Limit
	if jsonData.Page == -1 && jsonData.Limit == -1 {
		offsetVal = -1
	}
	tx := models.DB.Model(new(models.MMemberAddress)).Where("mid=?", jsonData.MID)
	if jsonData.ID > 0 {
		tx = tx.Where("id=?", jsonData.ID)
	}
	tx = tx.Count(&count).Limit(jsonData.Limit).Offset(offsetVal).Order("id desc").Find(&list)
	if tx.Error != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"success": true,
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

// 地址新增/修改
func AddEditAddress(c *gin.Context) {
	//接收
	var jsonData models.MMemberAddress
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		fmt.Println(err)
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	if jsonData.ID > 0 {
		//编辑
		err = models.DB.Model(new(models.MMemberAddress)).Where("id=?", jsonData.ID).Updates(&jsonData).Error
		if err != nil {
			define.Response.ResultFail(c, 10001, err.Error(), "")
			return
		}
	} else {
		//新增
		jsonData.Createtime = int(time.Now().Unix())
		err = models.DB.Model(new(models.MMemberAddress)).Create(&jsonData).Error
		if err != nil {
			define.Response.ResultFail(c, 10002, err.Error(), "")
			return
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"success": true,
		"data":    "",
		"msg":     "成功！",
	})
}

// 修改默认地址
func EditAddress(c *gin.Context) {
	var jsonData models.MMemberAddress
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		fmt.Println(err)
		define.Response.ResultFail(c, 10000, define.JsonData, "")
		return
	}
	var editjson = map[string]interface{}{
		"isdefault": 0,
	}
	err = models.DB.Model(new(models.MMemberAddress)).Where("mid=?", jsonData.Mid).Updates(&editjson).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, err.Error(), "")
		return
	}
	var editData = map[string]interface{}{
		"isdefault": 1,
	}
	err = models.DB.Model(new(models.MMemberAddress)).Where("id=?", jsonData.ID).Updates(&editData).Error
	if err != nil {
		define.Response.ResultFail(c, 10002, err.Error(), "")
		return
	}
	var userData = map[string]interface{}{
		"province":  jsonData.Province,
		"city":      jsonData.City,
		"area":      jsonData.District,
		"address":   jsonData.Area,
		"latitude":  jsonData.Latitude,
		"longitude": jsonData.Longitude,
	}
	err = models.DB.Model(new(models.MMember)).Where("id=?", jsonData.Mid).Updates(&userData).Error
	if err != nil {
		define.Response.ResultFail(c, 10003, err.Error(), "")
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"success": true,
		"data":    "",
		"msg":     "成功！",
	})
}

// 删除地址
func AddressDel(c *gin.Context) {
	var jsonData models.MMemberAddress
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		fmt.Println(err)
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	tx := models.DB.Begin()

	//删除
	err = tx.Delete(&models.MMemberAddress{}, jsonData.ID).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10004,
			"msg":  err.Error(),
		})
		tx.Rollback()
		return
	}
	tx.Commit()

	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"msg":     "成功！",
	})
}
